package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.Units;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

public class CompanyDashBoardUIController extends AbstractUIController
		implements Initializable {

	@FXML
	TabPane root;
	
	public static final String UNITS_TAB = "CompanyUnitsTab.fxml";
	public static final String EMPLOYEES_TAB = "CompanyEmployeesTab.fxml";
	public static final String DEPARTMENTS_TAB = "CompanyDepartmentsTab.fxml";
	public static final String DESIGNATIONS_TAB = "CompanyDesignationsTab.fxml";
	public static final String CATEGORIES_TAB = "CompanyEmployeeCategoriesTab.fxml";
	public static final String ATTENDANCE_TAB = "CompanyEmployeeAttendanceTab.fxml";
	public static final String Leave_TAB = "CompanyEmployeeLeaveTab.fxml";
	public static final String USER_ACCOUNTS_TAB = "UserAccountsHandler.fxml";
	public static final String SALARY_TAB = "SalaryTab.fxml";
	public static final String MISCELINIOUS_TAB = "MiscelliniousTab.fxml";
	
	private CompanyUnitsTabController companyUnitsTabController;
	private CompanyEmployeesTabController companyEmployeesTabController;
	private CompanyDepartmentsTabController companyDepartmentTabController;
	private CompanyDesignationsTabController companyDesignationsController;
	private CompanyCategoriesTabController companyCategoriesTabController;
	private CompanyAttendanceTabController companyAttendanceTabController;
	private CompanyLeaveTabController companyLeaveTabController;
	private UserAccountsHandlerController userAccountsHandlerController;
	private MisceliniousTabController misceliniousTabController;
	private SalaryTabController salaryTabController;
	
	private Company company = null;
	private UserAccounts userAccounts = null;

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		if (company != null) {
			return company.toString();
		} else {
			return null;
		}
	}

	@Override
	public void refreshUI(Object... args) {
		
		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = company;
			
			if(args.length > 1 && args[1] instanceof UserAccounts){
				userAccounts =  (UserAccounts) args[1];
			}
		}
		
		companyUnitsTabController.refreshUI(company,userAccounts);
		companyEmployeesTabController.refreshUI(company,userAccounts);
		companyDepartmentTabController.refreshUI(company,userAccounts);
		companyDesignationsController.refreshUI(company,userAccounts);
		companyCategoriesTabController.refreshUI(company,userAccounts);
		companyAttendanceTabController.refreshUI(company,userAccounts);
		companyLeaveTabController.refreshUI(company,userAccounts);
		userAccountsHandlerController.refreshUI(company,userAccounts);
		salaryTabController.refreshUI(company, userAccounts);
		misceliniousTabController.refreshUI(company,userAccounts);
	
	
	}
	

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {

		companyEmployeesTabController = (CompanyEmployeesTabController) loadTabController(EMPLOYEES_TAB);
		companyDepartmentTabController = (CompanyDepartmentsTabController) loadTabController(DEPARTMENTS_TAB);
		companyDesignationsController = (CompanyDesignationsTabController) loadTabController(DESIGNATIONS_TAB);
		companyCategoriesTabController = (CompanyCategoriesTabController) loadTabController(CATEGORIES_TAB);
		companyAttendanceTabController = (CompanyAttendanceTabController) loadTabController(ATTENDANCE_TAB);
		companyLeaveTabController = (CompanyLeaveTabController) loadTabController(Leave_TAB);
		userAccountsHandlerController =  (UserAccountsHandlerController) loadTabController(USER_ACCOUNTS_TAB);
		salaryTabController =  (SalaryTabController) loadTabController(SALARY_TAB);
		misceliniousTabController = (MisceliniousTabController) loadTabController(MISCELINIOUS_TAB);
		companyUnitsTabController = (CompanyUnitsTabController) loadTabController(UNITS_TAB);
		
		Tab unitsTab = new Tab("Units");
		unitsTab.closableProperty().set(false);
		unitsTab.setContent(companyUnitsTabController.getView());
		root.getTabs().add(unitsTab);

		Tab employeeTab = new Tab("Employee");
		employeeTab.closableProperty().set(false);
		employeeTab.setContent(companyEmployeesTabController.getView());
		root.getTabs().add(employeeTab);

		Tab departmentsTab = new Tab("Departments");
		departmentsTab.closableProperty().set(false);
		departmentsTab.setContent(companyDepartmentTabController.getView());
		root.getTabs().add(departmentsTab);

		Tab designationsTab = new Tab("Designations");
		designationsTab.closableProperty().set(false);
		designationsTab.setContent(companyDesignationsController.getView());
		root.getTabs().add(designationsTab);

		Tab categoriesTab = new Tab("Employee Categories");
		categoriesTab.closableProperty().set(false);
		categoriesTab.setContent(companyCategoriesTabController.getView());
		root.getTabs().add(categoriesTab);
		
		Tab attendanceTab = new Tab("Attendance");
		attendanceTab.closableProperty().set(false);
		attendanceTab.setContent(companyAttendanceTabController.getView());
		root.getTabs().add(attendanceTab);
		
		Tab leaveTab = new Tab("Leave");
		leaveTab.closableProperty().set(false);
		leaveTab.setContent(companyLeaveTabController.getView());
		root.getTabs().add(leaveTab);
		
		Tab userAccounts = new Tab("User Accounts");
		userAccounts.closableProperty().set(false);
		userAccounts.setContent(userAccountsHandlerController.getView());
		root.getTabs().add(userAccounts);
		
		Tab salaryTab = new Tab("Salary");
		salaryTab.closableProperty().set(false);
		salaryTab.setContent(salaryTabController.getView());
		root.getTabs().add(salaryTab);
		
		Tab miscelinious = new Tab("Others");
		miscelinious.closableProperty().set(false);
		miscelinious.setContent(misceliniousTabController.getView());
		root.getTabs().add(miscelinious);
	}

	private AbstractUIController loadTabController(String name) {
		try {
			FXMLLoader loader = new FXMLLoader();
			PayRollUtility.debug("name : "+name);
			loader.load(loader.getClass().getResourceAsStream("/fxml/" + name));
			AbstractUIController uiController = loader.getController();
			return uiController;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
